package org.jeecg.modules.demo.com.kunpeng.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.com.kunpeng.entity.CustomerInformation;
import org.jeecg.modules.demo.com.kunpeng.entity.OtherContract;
import org.jeecg.modules.demo.com.kunpeng.entity.OtherContractDetail;
import org.jeecg.modules.demo.com.kunpeng.entity.OtherContractCollectionPlan;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.OtherContractKeyWordsQueryDTO;
import org.jeecg.modules.demo.com.kunpeng.mapper.OtherContractDetailMapper;
import org.jeecg.modules.demo.com.kunpeng.mapper.OtherContractCollectionPlanMapper;
import org.jeecg.modules.demo.com.kunpeng.mapper.OtherContractMapper;
import org.jeecg.modules.demo.com.kunpeng.service.ICustomerInformationService;
import org.jeecg.modules.demo.com.kunpeng.service.IOtherContractService;
import org.jeecg.modules.demo.com.kunpeng.vo.CustomerInformationPage;
import org.jeecg.modules.demo.com.kunpeng.vo.OtherContractVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 其他合同表
 * @Author: jeecg-boot
 * @Date:   2022-12-23
 * @Version: V1.0
 */
@Service
@Slf4j
public class OtherContractServiceImpl extends ServiceImpl<OtherContractMapper, OtherContract> implements IOtherContractService {

	@Autowired
	private OtherContractMapper otherContractMapper;
	@Autowired
	private OtherContractDetailMapper otherContractDetailMapper;
	@Autowired
	private OtherContractCollectionPlanMapper otherContractCollectionPlanMapper;
	@Autowired
	private ICustomerInformationService customerInformationService;

	/**
	 * 插入其他合同表及关联表数据
	 * @param otherContract
	 * @param otherContractDetailList
	 * @param otherContractCollectionPlanList
	 * @param customerInformation
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(OtherContract otherContract, List<OtherContractDetail> otherContractDetailList, List<OtherContractCollectionPlan> otherContractCollectionPlanList, CustomerInformation customerInformation) {
		otherContract.setStatus("0");
		otherContractMapper.insert(otherContract);
		log.info("其他合同表数据插入成功！");
		CustomerInformationPage customerInformationPage=new CustomerInformationPage();
		BeanUtils.copyProperties(customerInformation,customerInformationPage);
		customerInformationService.insert(customerInformationPage);
		log.info("客户表数据插入成功！");
		if(otherContractDetailList!=null && otherContractDetailList.size()>0) {
			for(OtherContractDetail entity:otherContractDetailList) {
				//外键设置
				entity.setScid(otherContract.getId());
				otherContractDetailMapper.insert(entity);
				log.info("其他合同详情表数据插入成功！");
			}
		}
		if(otherContractCollectionPlanList!=null && otherContractCollectionPlanList.size()>0) {
			for(OtherContractCollectionPlan entity:otherContractCollectionPlanList) {
				//外键设置
				entity.setScid(otherContract.getId());
				otherContractCollectionPlanMapper.insert(entity);
				log.info("其他合同收款计划表数据插入成功！");
			}
		}
	}

	/**
	 * 编辑其他合同表及关联数据
	 * @param otherContract
	 * @param otherContractDetailList
	 * @param otherContractCollectionPlanList
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(OtherContract otherContract,List<OtherContractDetail> otherContractDetailList,List<OtherContractCollectionPlan> otherContractCollectionPlanList) {
		otherContractMapper.updateById(otherContract);
		
		//1.先删除子表数据
		otherContractDetailMapper.deleteByMainId(otherContract.getId());
		otherContractCollectionPlanMapper.deleteByMainId(otherContract.getId());
		
		//2.子表数据重新插入
		if(otherContractDetailList!=null && otherContractDetailList.size()>0) {
			for(OtherContractDetail entity:otherContractDetailList) {
				//外键设置
				entity.setScid(otherContract.getId());
				otherContractDetailMapper.insert(entity);
				log.info("其他合同详情表编辑完成！");
			}
		}
		if(otherContractCollectionPlanList!=null && otherContractCollectionPlanList.size()>0) {
			for(OtherContractCollectionPlan entity:otherContractCollectionPlanList) {
				//外键设置
				entity.setScid(otherContract.getId());
				otherContractCollectionPlanMapper.insert(entity);
			}
		}
	}

	/**
	 * 根据其他合同id删除数据
	 * @param id
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		otherContractDetailMapper.deleteByMainId(id);
		otherContractCollectionPlanMapper.deleteByMainId(id);
		otherContractMapper.deleteById(id);
	}

	/**
	 * 根据其他合同id批量删除数据
	 * @param idList
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			otherContractDetailMapper.deleteByMainId(id.toString());
			otherContractCollectionPlanMapper.deleteByMainId(id.toString());
			otherContractMapper.deleteById(id);
		}
	}

	/**
	 * 根据关键字查询数据
	 * @param page
	 * @param otherContractKeyWordsQueryDTO
	 * @return
	 */
	//TODO 三个合同表存在重复代码，后期可优化
	@Override
	public Page<OtherContractVO> listByKeyWords(Page<OtherContractVO> page, OtherContractKeyWordsQueryDTO otherContractKeyWordsQueryDTO) {
		log.info("开始处理根据关键字查询数据表信息：service");
		Page<OtherContractVO> list = page.setRecords(otherContractMapper.listByKeyWords(page, otherContractKeyWordsQueryDTO.getContractProperties(), otherContractKeyWordsQueryDTO.getStatus(),
				otherContractKeyWordsQueryDTO.getKeyWords()));
		//遍历其他合同
		for (OtherContractVO otherContract:list.getRecords()) {
			System.out.println("其他合同数据："+otherContract.toString());
			//其他合同不含税金额
			otherContract.setNoTaxAmount(otherContract.getContractAmount().multiply(new BigDecimal(otherContract.getTaxPoint())));
			List<OtherContractCollectionPlan> otherContractCollectionPlans = otherContractCollectionPlanMapper.selectByMainId(otherContract.getId());
			if(otherContractCollectionPlans.size()>0){
				BigDecimal returnAmount=new BigDecimal(0.00);
				for(OtherContractCollectionPlan saleContractCollectionPlan:otherContractCollectionPlans){
					log.info("其他合同收款计划数据："+saleContractCollectionPlan.toString());
					Long nowTimeMillis = System.currentTimeMillis();
					Calendar cal=Calendar.getInstance();
					cal.setTime(saleContractCollectionPlan.getAgreedCollectionTime());
					Long collectionMillis=cal.getTimeInMillis();
					if(nowTimeMillis>collectionMillis){
						returnAmount=returnAmount.add(saleContractCollectionPlan.getAmountCollected());
					}
				}
				//其他合同回款金额
				otherContract.setReturnAmount(returnAmount);
				//其他合同未回款金额
				otherContract.setNoReturnAmount(otherContract.getNoTaxAmount().subtract(returnAmount));
			}
			/*if (!(otherContract.getContractStartTime() == null || otherContract.getContractEndTime() == null)) {
				long nowTimeMillis = System.currentTimeMillis();
				Calendar cal = Calendar.getInstance();
				cal.setTime(otherContract.getContractStartTime());
				long startTimeMillis = cal.getTimeInMillis();
				cal.setTime(otherContract.getContractEndTime());
				Long endTimeMillis = cal.getTimeInMillis();
				Long seTime = endTimeMillis - startTimeMillis;
				Long snTime = nowTimeMillis - startTimeMillis;
				if (snTime > seTime) {
					otherContract.setStatus("已完成");
					otherContractMapper.updateStateById(otherContract.getId(),"3");
				} else if (startTimeMillis > nowTimeMillis) {
					otherContract.setStatus("未开始");
					otherContractMapper.updateStateById(otherContract.getId(), "1");
				} else if (seTime > snTime) {
					otherContract.setStatus("执行中");
					otherContractMapper.updateStateById(otherContract.getId(), "2");
				}
			}*/
			String status = otherContract.getStatus();
			switch (status){
				case "0":otherContract.setStatus("待审批");
					break;
				case "1":otherContract.setStatus("已审批");
					break;
				case "2":otherContract.setStatus("中止");
					break;
				case "3":otherContract.setStatus("作废");
					break;
			}
		}
		try {
			if(list.getRecords().size()==0);
		} catch (Exception e) {
			Result.error("数据不存在，请重新输入！");
			e.printStackTrace();
		}
		return list;
	}


	/**
	 * 根据id修改合同状态
	 * @param id
	 * @param status
	 */
	@Override
	public void updateStateById(String id, String status) {
		otherContractMapper.updateStateById(id,status);
	}
}
